<?php

declare(strict_types=1);

namespace App\Middleware;

use App\Constants\ErrorCode;
use App\Model\data\CurrentShopData;
use App\Service\FlyShopService;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Common\Http\Response;

/**
 * 商城中间件
 * Class BusinessShopMiddleware
 * @package App\Middleware
 */
class BusinessShopMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @var Response
     */
    protected $response;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->response = $container->get(Response::class);
        $this->request = $container->get(RequestInterface::class);

    }

    /**
     * 判断是什么店
     * @param ServerRequestInterface $request
     * @param RequestHandlerInterface $handler
     * @return ResponseInterface
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        //这是云商城
        if (CurrentShopData::getData()->shop_type != 2) {
            return $this->response->error(ErrorCode::UNAUTHORIZED, "无云商城权限!");
        }
        return $handler->handle($request);
    }
}